@ECHO OFF
IF NOT DEFINED M2_REPO GOTO NO_M2_REPO

ECHO Installing artifacts.
cmd /C mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file -Dfile=artifacts\script-maven-plugin-1.2.0-SNAPSHOT.jar -DpomFile=artifacts\pom.xml -Dsources=artifacts\script-maven-plugin-1.2.0-SNAPSHOT-sources.jar -Djavadoc=artifacts\script-maven-plugin-1.2.0-SNAPSHOT-javadoc.jar

IF NOT EXIST %M2_REPO%\gap\maven\maven-metadata-local.xml (GOTO COPY_FILE) ELSE (GOTO MERGE_FILE)
GOTO END
:NO_M2_REPO
	ECHO M2_REPO not defined.
	GOTO END

:COPY_FILE
	ECHO Copying metadata.
	copy artifacts\maven-metadata-local.xml %M2_REPO%\gap\maven\maven-metadata-local.xml
	GOTO UDPATE_LATEST_TAG

:MERGE_FILE
	ECHO Merging metadata.
	mkdir transform
	copy %M2_REPO%\gap\maven\maven-metadata-local.xml transform\maven-metadata-local-prefix.xml
	cmd /C mvn xml:transform
	copy transform\merged\maven-metadata-local-prefix.xml %M2_REPO%\gap\maven\maven-metadata-local.xml
	rmdir /Q /S transform
	GOTO UDPATE_LATEST_TAG

:UDPATE_LATEST_TAG
	ECHO Merging latest version.
	mkdir transform
	copy %M2_REPO%\gap\maven\script-maven-plugin\maven-metadata-local.xml transform\maven-metadata-local-latest.xml
	cmd /C mvn xml:transform
	copy transform\merged\maven-metadata-local-latest.xml %M2_REPO%\gap\maven\script-maven-plugin\maven-metadata-local.xml
	rmdir /Q /S transform
	GOTO END


:END
	IF ERRORLEVEL ==0 (
		ECHO Done.
	) ELSE (
		ECHO Failed.
	)

@ECHO ON
